package com.daqsoft.controller.process;

import com.daqsoft.data.Message;
import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.dto.TaskTypeEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.NoticeService;
import com.daqsoft.service.process.ProjectChangeProcessService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.service.system.UserManageService;
import com.daqsoft.util.DateUtil;
import com.daqsoft.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 项目变更流程控制器
 * 流程: 提交变更请求 -> 审核变更请求 -> 更改项目任务 -> 审核任务合理 -> 走项目确认流程 -> 完成
 * @author 周麟东 on 11:20
 */
@RestController
@RequestMapping("projectChangeProcess")
public class ProjectChangeProcessController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectChangeProcessService service;
    @Autowired
    private UserManageService userService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private NoticeService noticeService;

    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "创建变更申请提交任务")
    @RequestMapping("createChangeTask")
    public GeneralResultEnum createChangeTask(HttpServletRequest request, Task task, int projectId, int principalId) {
        User user = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null) {
            return GeneralResultEnum.FAIL.setDesc("项目不存在");
        }
        if (project.getStatus() != StatusEnum.PROCESS) {
            return GeneralResultEnum.FAIL.setDesc("该项目目前不能变更");
        }
        User verifier = userService.findOne(principalId);
        if (verifier == null) {
            return GeneralResultEnum.FAIL.setDesc("审核用户不存在");
        }
        task.setType(TaskTypeEnum.CHANGE);
        task.setProject(project);
        task.setPrincipal(user);
        task.setVerifier(verifier);
        task.setStatus(StatusEnum.CHECK);
        task.setName("变更请求发起");
        task.setMsg(Log.format(user,"<i>发起变更申请任务</i>"));
        task.setRealStartDate(new Date());
        task.setStartDate(new Date());
        task.setEndDate(DateUtil.INS.addDay(new Date(),3));
        task = taskService.save(task);
        return GeneralResultEnum.SUCCESS.setDesc("创建变更成功").setData(task.toMap());
    }


    @RequestMapping("createChangeProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "创建变更项目")
    public GeneralResultEnum createChangeProjectRequest(int previous){
        Project father = projectService.get(previous);
        if (father == null){
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        return GeneralResultEnum.SUCCESS.setData(service.createChangeProject(previous).toMap());
    }

    /**
     * 创建变更项目审核任务
     */
    @RequestMapping("createChangeProjectTask")
    @Verify(value = VerifyType.HAVE_PERMISSION, desc = "创建审核变更项目任务")
    public GeneralResultEnum createChangeProjectTask(HttpServletRequest request, int principalId, int projectId) {
        User verifier = userService.findOne(principalId);
        if (verifier == null) {
            return GeneralResultEnum.FAIL.setDesc("审核人不存在");
        }
        Project project = projectService.get(projectId);
        if (project == null) {
            return GeneralResultEnum.FAIL.setDesc("项目不存在");
        }
        User principal = (User) request.getSession().getAttribute("user");
        service.createChangeProjectTask(project, verifier, principal);
        //发送通知
        noticeService.sendMessage(new Message(verifier, principal, principal.getRealName() + "已拟定完成项目[" + project.getProjectName() + "]变更计划 , 请审核", "/index.html#tasks-panel"));
        return GeneralResultEnum.SUCCESS.setDesc("提交成功");
    }


    /**
     * 审核变更后的任务
     * @param projectId 项目ID(变更后)
     * @param pass 是否通过
     * @return 是否成功
     */
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "审核变更后任务")
    @RequestMapping("checkChangeProject")
    public GeneralResultEnum checkChangeProject(HttpServletRequest request , int projectId , boolean pass , int taskId , String msg){
        User user = (User) request.getSession().getAttribute("user");
        Project project = projectService.get(projectId);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("错误,项目ID无法找到:"+projectId);
        }
        Task task = taskService.get(taskId);
        if (task == null){
            return GeneralResultEnum.FAIL.setDesc("错误,任务ID无法找到:"+taskId);
        }
        task.setMsg(Log.add(user,task.getMsg(), (pass ? "<i>通过变更" : "<i>驳回变更") + ":" + msg));
        service.checkChangeProject(project , task , pass);
        //发送通知
        noticeService.sendMessage(new Message(task.getPrincipal() , task.getVerifier(),pass ? "项目变更计划通过审核" : "项目变更计划被驳回"));
        return GeneralResultEnum.SUCCESS.setDesc("审核完成");

    }

}
